package com.pigotech.ptwo.UI.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.pigotech.ptwo.R;
import com.pigotech.ptwo.widget.MediaController;
import com.pigotech.ptwo.widget.VideoView;

import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
 * Created by ghb on 2018/4/3.
 */

public class PlayBackActivity extends Activity{
    private VideoView mVideoView;
    private View mBufferingIndicator;
    private MediaController mMediaController;
    private String mVideoPath;
    private String videoName = "";
    public int statusBarHeight = -1;//系统状态栏高度
    private String TAG = "PlayBackActivity";
    private int screenWidth;
    private int screenHeigth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setFullScreen();
        setContentView(R.layout.activity_play_back);
        getScreenSize();
        getStatusBarHeight();
        setStatusBar();

        IjkMediaPlayer.loadLibrariesOnce(null);
        IjkMediaPlayer.native_profileBegin("libijkplayer.so");

        mVideoPath = "rtsp://192.168.42.1/live";
        //回放类似 http://192.168.42.1/SD0/NORMAL/M_video/PIONEER_20171125_112332A.MP4

        Intent intent = getIntent();
        String intentAction = intent.getAction();
        if (!TextUtils.isEmpty(intentAction) && intentAction.equals(Intent.ACTION_VIEW)) {
            mVideoPath = intent.getDataString();
        }
        videoName = intent.getStringExtra("video_name");
        if(!"".equals(videoName)) {
            mVideoPath = "http://192.168.42.1/SD0/NORMAL/M_video/" + videoName;
        }

        mBufferingIndicator = findViewById(R.id.buffering_indicator);
        mMediaController = new MediaController(this);

        mVideoView = (VideoView) findViewById(R.id.video_view);
        mVideoView.setMediaController(mMediaController);
        mVideoView.setMediaBufferingIndicator(mBufferingIndicator);
        mVideoView.setVideoPath(mVideoPath);
        mVideoView.requestFocus();
        mVideoView.start();
        mVideoView.setMediaController(null);
    }

    private void getScreenSize() {
        WindowManager wm = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        screenWidth = wm.getDefaultDisplay().getWidth();
        screenHeigth = wm.getDefaultDisplay().getHeight();
    }

    /**
     * 获取系统状态栏高度
     */
    private void getStatusBarHeight() {
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
    }
    /**
     *4.4系统以上开启沉浸式后应用标题防止与系统标题重叠
     */
    private void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            findViewById(R.id.title_view).setVisibility(View.VISIBLE);
            findViewById(R.id.title_view).getLayoutParams().height = 0;
//            findViewById(R.id.title_view).setVisibility(View.VISIBLE);
//            findViewById(R.id.title_view).getLayoutParams().height = statusBarHeight;
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明底部导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        IjkMediaPlayer.native_profileEnd();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == 1) {
            //竖屏
            Log.d(TAG,"竖屏");
            ViewGroup.LayoutParams lp = mVideoView.getLayoutParams();
            lp.width = screenWidth;
            lp.height = screenWidth * 9 / 16;
        }
        if(newConfig.orientation == 2) {
            //横屏
            Log.d(TAG,"横屏");
            ViewGroup.LayoutParams lp = mVideoView.getLayoutParams();
            lp.width = screenHeigth;
            lp.height = screenWidth;
        }
    }

    private void setFullScreen() {
        Window window = getWindow();
        //隐藏状态栏
        //定义全屏参数
        int flag=WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
    }
}
